@SET_MAKE@

srcdir = @srcdir@
VPATH = @srcdir@

top_srcdir = @top_srcdir@

include ../config.make

PRE_CPPFLAGS = -I.. -I$(top_srcdir)
PRE_LDFLAGS = -L..

TS_NETTLE_SOURCES = aes-test.c arcfour-test.c arctwo-test.c \
		    blowfish-test.c cast128-test.c \
	            base16-test.c base64-test.c \
		    camellia-test.c chacha-test.c \
		    cnd-memcpy-test.c \
		    des-test.c des3-test.c \
		    md2-test.c md4-test.c md5-test.c md5-compat-test.c \
		    memeql-test.c memxor-test.c gosthash94-test.c \
		    ripemd160-test.c hkdf-test.c \
		    salsa20-test.c \
		    sha1-test.c sha224-test.c sha256-test.c \
		    sha384-test.c sha512-test.c sha512-224-test.c sha512-256-test.c \
		    sha3-permute-test.c sha3-224-test.c sha3-256-test.c \
		    sha3-384-test.c sha3-512-test.c \
		    shake256-test.c \
		    serpent-test.c twofish-test.c version-test.c \
		    knuth-lfib-test.c \
		    cbc-test.c cfb-test.c ctr-test.c gcm-test.c eax-test.c ccm-test.c \
		    cmac-test.c siv-test.c \
		    poly1305-test.c chacha-poly1305-test.c \
		    hmac-test.c umac-test.c \
		    meta-hash-test.c meta-cipher-test.c\
		    meta-aead-test.c meta-armor-test.c meta-mac-test.c \
		    buffer-test.c yarrow-test.c xts-test.c pbkdf2-test.c \
		    x86-ibt-test.c

TS_HOGWEED_SOURCES = sexp-test.c sexp-format-test.c \
		     rsa2sexp-test.c sexp2rsa-test.c \
		     bignum-test.c random-prime-test.c \
		     pkcs1-test.c pkcs1-sec-decrypt-test.c \
		     pss-test.c rsa-sign-tr-test.c \
		     pss-mgf1-test.c rsa-pss-sign-tr-test.c \
		     rsa-test.c rsa-encrypt-test.c rsa-keygen-test.c \
		     rsa-sec-decrypt-test.c \
		     rsa-compute-root-test.c \
		     dsa-test.c dsa-keygen-test.c \
		     curve25519-dh-test.c curve448-dh-test.c \
		     ecc-mod-test.c ecc-modinv-test.c ecc-redc-test.c \
		     ecc-sqrt-test.c \
		     ecc-dup-test.c ecc-add-test.c \
		     ecc-mul-g-test.c ecc-mul-a-test.c \
		     ecdsa-sign-test.c ecdsa-verify-test.c \
		     ecdsa-keygen-test.c ecdh-test.c \
		     eddsa-compress-test.c eddsa-sign-test.c \
		     eddsa-verify-test.c ed25519-test.c ed448-test.c \
		     gostdsa-sign-test.c gostdsa-verify-test.c \
		     gostdsa-keygen-test.c gostdsa-vko-test.c

TS_SOURCES = $(TS_NETTLE_SOURCES) $(TS_HOGWEED_SOURCES)
CXX_SOURCES = cxx-test.cxx

TS_NETTLE = $(TS_NETTLE_SOURCES:.c=$(EXEEXT))
TS_HOGWEED = $(TS_HOGWEED_SOURCES:.c=$(EXEEXT))
TS_C = $(TS_NETTLE) @IF_HOGWEED@ $(TS_HOGWEED)
TS_CXX = @IF_CXX@ $(CXX_SOURCES:.cxx=$(EXEEXT))
TARGETS = $(TS_C) $(TS_CXX)
TS_SH = sexp-conv-test pkcs1-conv-test nettle-pbkdf2-test symbols-test
TS_ALL = $(TARGETS) $(TS_SH) @IF_DLOPEN_TEST@ dlopen-test$(EXEEXT)
EXTRA_SOURCES = sha1-huge-test.c
EXTRA_TARGETS = $(EXTRA_SOURCES:.c=$(EXEEXT))


# Includes all C source files, regardless of configuration
SOURCES = $(TS_SOURCES) $(EXTRA_SOURCES) testutils.c dlopen-test.c

DISTFILES = $(SOURCES) $(CXX_SOURCES) Makefile.in \
	    $(TS_SH) teardown-env \
	    gold-bug.txt testutils.h sha3.awk

all: $(EXTRA_TARGETS)

%.$(OBJEXT): %.c
	$(COMPILE) -c $< && $(DEP_PROCESS)

%.$(OBJEXT): %.cxx
	$(COMPILE_CXX) -c $< && $(DEP_PROCESS)

# BSD (and Solaris) make doesn't allow extra dependencies together one
# single-suffix rules, which makes it impossible or almost impossible
# to use suffix rules to build the test executables. So we use an
# explicit rule for each and every executable.

TEST_OBJS = testutils.$(OBJEXT) ../nettle-internal.$(OBJEXT)
LIB_HOGWEED = @IF_HOGWEED@ -lhogweed
TEST_LIBS = $(LIB_HOGWEED) -lnettle $(LIBS)

../nettle-internal.$(OBJEXT):
	( cd .. && $(MAKE) nettle-internal.$(OBJEXT) )

# Special target, to omit linking with libnettle
dlopen-test$(EXEEXT): dlopen-test.$(OBJEXT) testutils.$(OBJEXT)
	$(LINK) dlopen-test.$(OBJEXT) -ldl -o dlopen-test$(EXEEXT)

# Special target, using C++ linking
cxx-test$(EXEEXT): cxx-test.$(OBJEXT)
	$(LINK_CXX) $< $(TEST_OBJS) $(TEST_LIBS) -o $@

%$(EXEEXT): %.$(OBJEXT)
	$(LINK) $< $(TEST_OBJS) $(TEST_LIBS) -o $@

$(TARGETS) $(EXTRA_TARGETS): testutils.$(OBJEXT) ../nettle-internal.$(OBJEXT) \
	../libnettle.stamp @IF_HOGWEED@ ../libhogweed.stamp

# For use as, e.g.,
#
#   make check EMULATOR='$(VALGRIND)'
#   make check EMULATOR='$(VALGRIND) --log-fd=3' 3>valgrind.log

# --partial-loads-ok=yes is needed for memxor's handling of unaligned
# data.
VALGRIND = valgrind --error-exitcode=1 --leak-check=full --show-reachable=yes @IF_ASM@ --partial-loads-ok=yes

check: $(TS_ALL)
	TEST_SHLIB_DIR="$(TEST_SHLIB_DIR)" \
	  srcdir="$(srcdir)" \
	  EMULATOR="$(EMULATOR)" NM="$(NM)" EXEEXT="$(EXEEXT)" \
          $(top_srcdir)/run-tests $(TS_ALL)


Makefile: $(srcdir)/Makefile.in ../config.status
	cd .. && $(SHELL) ./config.status testsuite/$@

install uninstall:
	true

distdir: $(DISTFILES)
	cp $? $(distdir)

clean:
	-rm -f $(TARGETS) $(EXTRA_TARGETS) dlopen-test$(EXEEXT) \
		*.$(OBJEXT) *.$(OBJEXT).d test.in test1.out test2.out

distclean: clean
	-rm -f Makefile *.d

tags:
	etags -o $(srcdir)/TAGS --include $(top_srcdir) $(srcdir)/*.c $(srcdir)/*.h

# Includes dependency files for everything, including objects which
# the current configuration will not build.
DEP_FILES = $(SOURCES:.c=.$(OBJEXT).d) $(CXX_SOURCES:.cxx=.$(OBJEXT).d)
-include $(DEP_FILES)
